//
//  main.m
//  C9DynaLoader
//
//  Created by 花强 on 2017/12/18.
//  Copyright © 2017年 花强. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "C9BasicGreeter.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        id greeter = [[C9BasicGreeter alloc] init];
        NSLog(@"%@",[greeter greeting:@"Hello-1"]);
        NSString * bundleGreeterPath;
        if (argc != 2) {
            NSLog(@"没有获得包路径！");
        }else{
            bundleGreeterPath = [NSString stringWithUTF8String:argv[1]];
            if (bundleGreeterPath == nil) {
                NSLog(@"Bundle not found at this path!");
            }else{
                id<C9Greeter> greeterB;
                NSBundle * greeterBundle = [NSBundle bundleWithPath:bundleGreeterPath];
                if (greeterBundle == nil) {
                    NSLog(@"Bundle not found at this path!");
                }else{
                    NSError * error;
                    BOOL isLoaded = [greeterBundle loadAndReturnError:&error];
                    if (!isLoaded) {
                        NSLog(@"Error = %@",[error localizedDescription]);
                    }else{
                        //加载包后使用该包创建一个对象并向这个对象发送一条消息
                        Class greeterClass = [greeterBundle classNamed:@"C9CustomGreeter"];
                        greeterB = [[greeterClass alloc] init];
                        NSLog(@"%@",[greeterB greeting:@"Hello-2"]);
                        
                        //使用完以动态方式加载的包，现在卸载它
                        //先释放所有用包中类创建的对象！
                        greeterB = nil;
                        BOOL isUnloaded = [greeterBundle unload];
                        if (!isUnloaded) {
                            NSLog(@"Counld not unload bundle!");
                        }
                    }
                }
            }
        }
        return 0;
    }
}
